package com.qitianyong.selfclass;


import java.util.LinkedList;

/**
 * Created by gy on 2017/4/28.
 */

public class VideoDataControl {
    private final int MAXMEMONRY = (int) (Runtime.getRuntime().maxMemory()) / 3;

    public LinkedList<VideoData> videoDatas = new LinkedList<>();
    int sum = 0;

    public VideoDataControl() {
        synchronized (VideoDataControl.class) {
            videoDatas.clear();
        }
    }

    public boolean isEmpty() {
        synchronized (VideoDataControl.class) {
            return videoDatas.isEmpty();
        }
    }

    public VideoData getFirst() {
        synchronized (VideoDataControl.class) {

            return videoDatas.getFirst();
        }
    }

    public void removeFirst() {
        synchronized (VideoDataControl.class) {
            sum -= videoDatas.removeFirst().length;
        }
    }

    public void addLast(VideoData videoData) {
        synchronized (VideoDataControl.class) {
            if (sum + videoData.length < MAXMEMONRY) {
                videoDatas.addLast(videoData);
                sum += videoData.length;
            }
//            Log.d("VideoData","MAXMEMONRY="+MAXMEMONRY+"  sum = "+sum+"  size = "+videoDatas.size()+"  videoData="+videoData.length);

        }
    }

    public void clear() {
        synchronized (VideoDataControl.class) {
            videoDatas.clear();
        }
    }

    public static class VideoData {
        public byte[] data;
        public int length;
        public int time;
        public  int width;
        public  int height;

        public VideoData(byte[] data, int length, int time, int width, int height) {
            this.data = data;
            this.length = length;
            this.time = time;
            this.width = width;
            this.height = height;
        }

        @Override
        public String toString() {
            return "VideoData{" +
                    ", length=" + length +
                    ", time=" + time +
                    ", width=" + width +
                    ", height=" + height +
                    '}';
        }
    }
}
